package com.itheima.socket.udp.demo03;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.SocketException;

public class ReceiveDataThread implements Runnable {
    @Override
    public void run() {

        try {
            DatagramSocket datagramSocket = new DatagramSocket(7777);
            while (true) {
                byte[] bytes = new byte[1024];
                int length = bytes.length;
                DatagramPacket datagramPacket = new DatagramPacket(bytes, length);
                datagramSocket.receive(datagramPacket);
                byte[] data = datagramPacket.getData();
                int length1 = data.length;
                System.out.println(Thread.currentThread().getName() + "说：" + new String(bytes, 0, length1, "UTF-8"));
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
